package com.penghe.www.baselibrary.ui.activities

import android.os.Bundle
import android.support.annotation.StringRes
import android.support.design.widget.NavigationView
import android.support.v4.view.GravityCompat
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import com.penghe.www.baselibrary.R


/**
 * Created by alm on 17-6-1.
 * 带有侧边栏的Activity父类
 */

abstract class BaseDrawerActivity : AppCompatActivity() {

    private var TAG: String? = null
    private var toolbar: Toolbar? = null
    private var drawer: DrawerLayout? = null
    private var drawerListener: DrawerLayout.DrawerListener? = null
    private var toggle: ActionBarDrawerToggle? = null
    private var menuResId: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        menuResId = -1
        TAG = "BaseDrawerActivity"
        drawerListener = object : DrawerLayout.DrawerListener {
            override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
                drawerSlide(drawerView, slideOffset)
            }

            override fun onDrawerOpened(drawerView: View) {
                drawerOpened(drawerView)
            }

            override fun onDrawerClosed(drawerView: View) {
                drawerClosed(drawerView)
            }

            override fun onDrawerStateChanged(newState: Int) {
                drawerStateChanged(newState)
            }
        }
        doBeforeSetLayout()

        setContentView(R.layout.activity_base_drawer)

        toolbar = findViewById(R.id.toolbar) as Toolbar
        setSupportActionBar(toolbar)
        drawer = findViewById(R.id.drawer_layout) as DrawerLayout
        toggle = ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer!!.addDrawerListener(toggle!!)
        drawer!!.addDrawerListener(drawerListener!!)
        toggle!!.syncState()
        val navigationView = findViewById(R.id.nav_view) as NavigationView
        navigationView.setNavigationItemSelectedListener({ this.navigationItemSelected(it) })
        val content = findViewById(R.id.content) as FrameLayout
        val view = layoutInflater.inflate(setLayout(), null)
        content.addView(view)

        doBeforeInitOthers()
        initViews()
        initViewData()
        initOtherData()
        initEvents()
    }


    override fun onBackPressed() {
        if (drawer!!.isDrawerOpen(GravityCompat.START)) {
            drawer!!.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        drawerListener = null
        toggle = null
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        return createOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return optionsItemSelected(item)
    }

    /**
     * 在设置布局之前需要进行的操作
     */
    protected abstract fun doBeforeSetLayout()

    /**
     * 设置布局

     * @return 布局id
     */
    protected abstract fun setLayout(): Int

    /**
     * 在设置布局之后，进行其他操作之前，所需要初始化的数据
     */
    protected abstract fun doBeforeInitOthers()

    /**
     * 初始化布局控件
     */
    protected abstract fun initViews()

    /**
     * 初始化控件数据
     */
    protected abstract fun initViewData()

    /**
     * 初始化其他数据
     */
    protected abstract fun initOtherData()

    /**
     * 初始化事件
     */
    protected abstract fun initEvents()

    /**
     * 设置菜单

     * @param menu 菜单
     * *
     * @return 只是重写 public boolean onCreateOptionsMenu(Menu menu)
     */
    protected abstract fun createOptionsMenu(menu: Menu): Boolean

    /**
     * 设置菜单监听

     * @param item 菜单的item
     * *
     * @return true表示处理了监听事件
     */
    protected abstract fun optionsItemSelected(item: MenuItem): Boolean

    /**
     * 侧边栏的监听

     * @param menuItem 侧边栏item
     * *
     * @return true表示处理了监听事件
     */
    protected abstract fun navigationItemSelected(menuItem: MenuItem): Boolean

    /**
     * DrawerLayout的滑动监听

     * @param drawerView  View
     * *
     * @param slideOffset slideOffset
     */
    protected abstract fun drawerSlide(drawerView: View, slideOffset: Float)

    /**
     * DrawerLayout已经完全打开了

     * @param drawerView View
     */
    protected abstract fun drawerOpened(drawerView: View)

    /**
     * DrawerLayout已经完全关闭了

     * @param drawerView View
     */
    protected abstract fun drawerClosed(drawerView: View)

    /**
     * DrawerLayout的状态改变了

     * @param newState 新的状态
     */
    protected abstract fun drawerStateChanged(newState: Int)

    //下面是自定义的一些常用的功能集成的函数，上方都是一些回调与重写的函数

    /**
     * 隐藏标题栏的返回按钮
     */
    protected fun hideTitleBackButton() {
        toolbar!!.navigationIcon = null
    }

    /**
     * 显示标题栏的返回按钮
     */
    protected fun showTitleBackButton() {
        toolbar!!.setNavigationIcon(R.drawable.back)
    }

    /**
     * 隐藏标题栏
     */
    protected fun hideTitle() {
        val supportActionBar = supportActionBar ?: throw RuntimeException("supportActionBar is null")
        supportActionBar.hide()
    }

    /**
     * 显示标题栏
     */
    protected fun showTitle() {
        val supportActionBar = supportActionBar ?: throw RuntimeException("supportActionBar is null")
        supportActionBar.show()
    }

    /**
     * 关闭侧边栏
     */
    protected fun closeDrawer() {
        if (drawer!!.isDrawerOpen(GravityCompat.START)) {
            drawer!!.closeDrawer(GravityCompat.START)
        }
    }

    /**
     * 设置标题栏的内容

     * @param titleResId 标题栏的资源id
     */
    protected fun setTitleText(@StringRes titleResId: Int) {
        val supportActionBar = supportActionBar ?: throw RuntimeException("BaseAppcompatActivity.setTitleText() must be do after method BaseAppcompatActivity.setLayout()")
        supportActionBar.setTitle(titleResId)
    }

    /**
     * 设置标题栏的内容

     * @param titleText 标题栏内容
     */
    protected fun setTitleText(titleText: String) {
        val supportActionBar = supportActionBar ?: throw RuntimeException("BaseAppcompatActivity.setTitleText() must be do after method BaseAppcompatActivity.setLayout()")
        supportActionBar.title = titleText
    }
}